package cn.pconline.autoclub.web.bbs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubLevel;
import cn.pconline.autoclub.entity.ClubStat;
import cn.pconline.autoclub.entity.Theme;
import cn.pconline.autoclub.service.ClubLevelService;
import cn.pconline.autoclub.service.ClubService;
import cn.pconline.autoclub.service.StatService;
import cn.pconline.autoclub.service.ThemeService;
import cn.pconline.autoclub.util.EncodeUtils;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.autoclub.util.WebUtils;
import cn.pconline.autoclub.util.app.AppControllerStatic;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouruxuan
 * @date 2014年12月23日 上午11:50:45
 */
@Controller
@RequestMapping(value = "/bbsapi")
public class BbsController {
	
	@Autowired
	private ClubService clubService;
	@Autowired
	private ThemeService themeService;
	@Autowired
	private ClubLevelService clubLevelService;
	@Autowired
	private StatService statService;

	/** 当前接口版本号1.0 */
	private static final String V1_VERSION = "v1.0";
	/** 当前接口版本号1.0为最新的版本 */
	private static final String V1_VERSION_LATEST = "v1.0(latest)";

	/**
	 * 根据版块ID，获取车友会列表接口
	 * 
	 * @param req
	 * @param resp
	 */
	@ResponseBody
	@RequestMapping(value = "/1.0/club/getClubListByFourmId.do", method = RequestMethod.GET)
	public void getClubListByFourmId(HttpServletRequest request,
			HttpServletResponse response) {

		WebUtils.setEncoding(request, response);
		WebUtils.setCacheHeader(response, 3600);// 缓存 1h

		//获取参数
		long forumId = WebUtils.paramLong(request, "forumId", 0);
		String callback = AppControllerStatic.paramCallback(request);
		int xssEncode = AppControllerStatic.paramXssEncode(request);
		int pageNo = AppControllerStatic.paramPageNo(request);
		int pageSize = AppControllerStatic.paramPageSize(request);

		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();

		//参数判断
		if (forumId == 0) {
			AppControllerStatic.printErrorMsg(response, "非法参数forumId",
					jsonObject, callback, V1_VERSION_LATEST);
			return;
		}
		
		//判断是否有相关的主题
		List<Long> themeIds = new ArrayList<Long>();
		List<Theme> themeList = themeService.listThemeByForumId(forumId);
		for(Theme theme : themeList) {
			themeIds.add(theme.getThemeId());
		}
		
		//没有找到与此forumId相符合的主题
		if(themeIds.isEmpty()) {
			AppControllerStatic.printErrorMsg(response, "没有找到与此forumId相符合的主题",
					jsonObject, callback, V1_VERSION_LATEST);
			return;
		}
		
		//获取车友会列表，组装Json数据
		Pager<Club> pager = clubService.pageClubByThemeIds(themeIds.toArray(), pageNo, pageSize);
		if (!pager.getResultList().isEmpty()) {
			for (Club club : pager.getResultList()) {
				JSONObject json = new JSONObject();
				json.put("clubId", club.getClubId());
				json.put("vipSum", club.getVipNum());
				json.put("level", club.getLevelId());
				json.put("memberSum", club.getMemberNum());
				json.put("qqGroup", club.getQqGroup());
				json.put("clubUrl", club.getClubUrl());
				json.put("clubStatus", club.getStatus());
				json.put("brandId", club.getBrandId());
				json.put("rank", club.getRank());
				json.put("regionRank", club.getRegionRank());
				json.put("provinceRank", club.getProvinceRank());
				json.put("brandRank", club.getBrandRank());
				
				ClubLevel clubLevel = clubLevelService.getById(club.getLevelId());
				if(xssEncode > 0) {
					json.put("name", EncodeUtils.encodeForHTMLFilterNull(club.getName()));
					json.put("logo", EncodeUtils.encodeForHTMLFilterNull(club.getLogo()));
					json.put("brandName", EncodeUtils.encodeForHTMLFilterNull(club.getBrandName()));
					json.put("seriesName", EncodeUtils.encodeForHTMLFilterNull(club.getSeriesName()));
					json.put("provinceName", EncodeUtils.encodeForHTMLFilterNull(club.getProvinceName()));
					json.put("cityName", EncodeUtils.encodeForHTMLFilterNull(club.getCityName()));
					json.put("introduce", EncodeUtils.encodeForHTMLFilterNull(club.getIntroduce()));
					json.put("levelName", EncodeUtils.encodeForHTMLFilterNull(clubLevel == null ? "" : clubLevel.getLevelName()));
				} else {
					json.put("name", club.getName());
					json.put("logo", club.getLogo());
					json.put("brandName", club.getBrandName());
					json.put("seriesName", club.getSeriesName());
					json.put("provinceName", club.getProvinceName());
					json.put("cityName", club.getCityName());
					json.put("introduce", club.getIntroduce());
					json.put("levelName", clubLevel == null ? "" : clubLevel.getLevelName());
				}
				
				// 根据clubId得到活跃度值
				ClubStat clubStat = statService.getClubStatByClubId(club.getClubId());
				json.put("rankPetrol", clubStat == null ? "" : clubStat.getRankPetrol());
				
				jsonArray.add(json);
			}
		} 
		
		//返回数据
		jsonObject.put("clubList", jsonArray);
		AppControllerStatic.printSuccessMsgPager(response, jsonObject, callback, pager, V1_VERSION_LATEST);
	}
	
	/**
    * 获取车友会列表接口
    * 原接口 /club/getClubListForBBS.do
    * 
    * @throws IOException
    */
   @ResponseBody
   @RequestMapping(value = "/1.0/club/getClubListForBBS.do", method = RequestMethod.GET)
   public void getClubListForBBS(HttpServletRequest request, HttpServletResponse response) throws IOException {
       WebUtils.setEncoding(request, response);
       WebUtils.setCacheHeader(response, 3600);//缓存 1h
       //获取相关参数
       String callback = AppControllerStatic.paramCallback(request);
       long cityId = AppControllerStatic.paramCityId(request);
       long provinceId = AppControllerStatic.paramProvinceId(request);
       String[] seriesIds = WebUtils.paramTrim(request, "seriesIds", "").split(",");
       long brandId = AppControllerStatic.paramBrandId(request);
       int pageNo = AppControllerStatic.paramPageNo(request);
       int pageSize = AppControllerStatic.paramPageSize(request);
       int xssEncode = AppControllerStatic.paramXssEncode(request);
       int themeId = WebUtils.paramInt(request, "themeId", 0);//主题ID List<Object> sids = new ArrayList<Object>(seriesIds.length);
       //解释车友ID列表
       List<Object> sids = new ArrayList<Object>(seriesIds.length);
       for (String s : seriesIds) {
           try {
               int i = Integer.parseInt(s);
               if (i > 0) {
                   sids.add(i);
               }
           } catch (java.lang.NumberFormatException e) {
               continue;
           }
       }
       
       JSONObject jsonObject = new JSONObject();
       JSONArray jsonArray = new JSONArray();
       
       //获取车友会方法，组装Json
       Pager<Club> pager = clubService.pageClubByPlaceId(cityId, 0, provinceId, brandId, sids.toArray(), 0, null, pageNo, pageSize, themeId);
       if (!pager.getResultList().isEmpty()) {
			for (Club club : pager.getResultList()) {
				JSONObject json = new JSONObject();
				json.put("clubId", club.getClubId());
				json.put("vipSum", club.getVipNum());
				json.put("level", club.getLevelId());
				json.put("memberSum", club.getMemberNum());
				json.put("qqGroup", club.getQqGroup());
				json.put("clubUrl", club.getClubUrl());
				json.put("clubStatus", club.getStatus());
				json.put("brandId", club.getBrandId());
				json.put("rank", club.getRank());
				json.put("regionRank", club.getRegionRank());
				json.put("provinceRank", club.getProvinceRank());
				json.put("brandRank", club.getBrandRank());
				
				ClubLevel clubLevel = clubLevelService.getById(club.getLevelId());
				if(xssEncode > 0) {
					json.put("name", EncodeUtils.encodeForHTMLFilterNull(club.getName()));
					json.put("logo", EncodeUtils.encodeForHTMLFilterNull(club.getLogo()));
					json.put("brandName", EncodeUtils.encodeForHTMLFilterNull(club.getBrandName()));
					json.put("seriesName", EncodeUtils.encodeForHTMLFilterNull(club.getSeriesName()));
					json.put("provinceName", EncodeUtils.encodeForHTMLFilterNull(club.getProvinceName()));
					json.put("cityName", EncodeUtils.encodeForHTMLFilterNull(club.getCityName()));
					json.put("introduce", EncodeUtils.encodeForHTMLFilterNull(club.getIntroduce()));
					json.put("levelName", EncodeUtils.encodeForHTMLFilterNull(clubLevel == null ? "" : clubLevel.getLevelName()));
				} else {
					json.put("name", club.getName());
					json.put("logo", club.getLogo());
					json.put("brandName", club.getBrandName());
					json.put("seriesName", club.getSeriesName());
					json.put("provinceName", club.getProvinceName());
					json.put("cityName", club.getCityName());
					json.put("introduce", club.getIntroduce());
					json.put("levelName", clubLevel == null ? "" : clubLevel.getLevelName());
				}
				
				// 根据clubId得到活跃度值
				ClubStat clubStat = statService.getClubStatByClubId(club.getClubId());
				json.put("rankPetrol", clubStat == null ? "" : clubStat.getRankPetrol());
				
				jsonArray.add(json);
			}
		} 
		
		//返回数据
		jsonObject.put("clubList", jsonArray);
		AppControllerStatic.printSuccessMsgPager(response, jsonObject, callback, pager, V1_VERSION_LATEST);
   }
}
